﻿package skyExt
{
	import flash.display.*;
	import flash.events.*;
	import flash.net.*;
	import flash.utils.ByteArray;

	public class SkyScoreRank
	{
		//ip地址
		private static const HOST:String = "http://freesky.51mrp.com/";
		//排行榜数据返回
		private static var datas:Array = null  ;
		//当前用户排行
		private static var userRank:Array = null;
		//排行榜urlLoader
		private static var urlLoader:URLLoader ;
		//当前用户排行的urlLoader
		private static var urlPosLoader:URLLoader;
		//用户昵称
		private static var userName:String = "";
		
		//排行榜回调函数
		private static var cbk:*;
		
		

		/*
		uploadScore 上传分数。
		
		应用调用uploadScore传递参数
		userId:用户昵称，
		score：用户分数，
		startPos:返回排行榜数据的起始位置，
		endPos：返回排行榜数据的结束位置
		
		
		cbk是数据返回接口
		cbk(result:Array,userRank:Array): result是包含从startPos到endPos的对象数组，对象有俩个属性name：用户昵称，score：分数;
										 userRank是包含当前用户排行object对象，对象有一个属性postion
		返回null表示连接失败
		*/

		static public function uploadScore(userId:String,score:int,startPos:int,endPos:int,cbk: * ):void
		{
			urlLoader = new URLLoader();
			
			userName = userId;
			
			
			
			var url:String = HOST + "/flash/playerScore";

			var postData:String = "";

			postData +=  userId + "\n";

			postData +=  score + "\n";

			postData +=  startPos + "\n";

			postData +=  endPos + "\n";

			var urlRequest:URLRequest = new URLRequest(url);

			urlRequest.method = URLRequestMethod.POST;

			urlRequest.data = postData;
			urlLoader.addEventListener(Event.COMPLETE,SkyScoreRank.onLoadComplete);
			urlLoader.addEventListener(IOErrorEvent.IO_ERROR,SkyScoreRank.onLoadError);

			SkyScoreRank.cbk = cbk;

			urlLoader.dataFormat = URLLoaderDataFormat.TEXT;

			urlLoader.load(urlRequest);

		}
		/*
		getScoreRank 积分榜。
		
		应用调用getScoreRank传递参数
		startPos:返回排行榜数据的起始位置，
		endPos：返回排行榜数据的结束位置
		
		
		cbk是数据返回接口
		cbk(result:Array,userRank:Array): result是包含从startPos到endPos的对象数组，对象有俩个属性name：用户昵称，score：分数
										 getScoreRank中，userRank是null
		返回null表示连接失败
		*/

		static public function getScoreRank(startPos:int,endPos:int,cbk: * ):void
		{
			urlLoader = new URLLoader();
			
			userName = "";
			
			var url:String = HOST + "/flash/playerScore/getList";

			var postData:String = "";

			postData +=  startPos + "\n";
			postData +=  endPos + "\n";


			var urlRequest:URLRequest = new URLRequest(url);

			urlRequest.method = URLRequestMethod.POST;

			urlRequest.data = postData;


			urlLoader.addEventListener(Event.COMPLETE,SkyScoreRank.onLoadComplete);
			urlLoader.addEventListener(IOErrorEvent.IO_ERROR,SkyScoreRank.onLoadError);

			SkyScoreRank.cbk = cbk;

			urlLoader.dataFormat = URLLoaderDataFormat.TEXT;

			urlLoader.load(urlRequest);

		}

		/**
		获取当前用户的排名
		*/

		static private function getUserRank():void
		{
			urlPosLoader = new URLLoader();
			
			var url:String = HOST + "/flash/playerScore/getScore";

			var postData:String = "";

			postData +=  userName + "\n";
			
			var urlRequest:URLRequest = new URLRequest(url);

			urlRequest.method = URLRequestMethod.POST;

			urlRequest.data = postData;


			urlPosLoader.addEventListener(Event.COMPLETE,SkyScoreRank.onLoadComplete1);
			urlPosLoader.addEventListener(IOErrorEvent.IO_ERROR,SkyScoreRank.onLoadError);

			urlPosLoader.dataFormat = URLLoaderDataFormat.TEXT;

			urlPosLoader.load(urlRequest);
		}

		/**
		获取当前用户排行
		*/
		static private function onLoadComplete1(event:Event):void
		{
			var result:String = urlPosLoader.data;
			
			var isGetUserScore:Boolean = true;
			if (((result != null) && result != ""))
			{
				userRank = null;
				userRank = new Array();

				analizedResult(result,isGetUserScore);

			}
			
			SkyScoreRank.cbk(datas,userRank);
			
			urlPosLoader = null;
			
			datas = null;
			
			userRank = null;
		}


		/**
		获取数据
		*/

		static private function onLoadComplete(event:Event):void
		{
			var result:String = urlLoader.data;
			var isGetUserScore:Boolean = false;
			if (((result != null) && result != ""))
			{
				datas = null;

				datas = new Array  ;

				analizedResult(result,isGetUserScore);
				
				
				//如果是上传积分
				
				
				urlLoader = null;
				
				if(userName != "")
				{
					SkyScoreRank.getUserRank();
				}
				else//获取积分排行榜
				{
					SkyScoreRank.cbk(datas,userRank);
					
					datas = null;
			
					userRank = null;
				}
			}
			else
			{
				urlLoader = null;
				SkyScoreRank.cbk(null,null);
			}
			
		
		}

		/**
		连接错误
		*/

		static private function onLoadError(event:Event):void
		{
			SkyScoreRank.cbk(null,null);
			
			urlLoader = null;
			
			urlPosLoader = null;
		}

		/**
		解析返回数据
		*/
		static private function analizedResult(result:String,isGetUserSocre:Boolean):void
		{
			var startPos:int,endPos:int;
			var pos:String;

			if (! isGetUserSocre)
			{
				var scores:Array = result.split(";");

				for (var i:int = 0; i < scores.length; i++)
				{
					if (scores[i] != null && scores[i] != "")
					{
						var userName:String;
						var iScore:String;

						startPos = scores[i].indexOf(":");
						endPos = scores[i].indexOf(",");

						userName = scores[i].substring((startPos + 2),endPos - 1);

						startPos = scores[i].lastIndexOf(":");
						endPos = scores[i].lastIndexOf("}");

						iScore = scores[i].substring((startPos + 2),endPos - 1);

						datas.push({name:userName,score:iScore});
					}
				}
			}
			else
			{
				var userPositon:String = result;

				startPos = userPositon.lastIndexOf(":");
				endPos = userPositon.lastIndexOf("}");

				pos = userPositon.substring((startPos + 2),endPos - 1);

				userRank.push( {position:pos});			
			}
		}
	}

}